package cn.com.fuhuitong.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import java.lang.Exception
import java.util.LinkedHashSet

open class BaseDelegateAdapter<T> internal constructor(
    private val mContext: Context,
    private val mHelper: LayoutHelper,
    private val mLayoutId: Int = 1,
    private val mItemCount: Int = 0,
    private val mItemViewType: Int = 0
) : DelegateAdapter.Adapter<RecyclerViewHolder?>() {


    private var onItemClickListener: ((View, Int, T) -> Unit)? = null
    private var onLongItemClickListener: ((View, Int, T) -> Boolean)? = null
    private var onItemChildClickListener: ((View, Int, T) -> Unit)? = null
    private var onLongItemChildClickListener: ((View, Int, T) -> Boolean)? = null

    /**
     * 用于保存需要设置点击事件的 item
     */
    private val childClickViewIds = LinkedHashSet<Int>()

    val data: MutableList<T> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        if (viewType == mItemViewType) {
            return RecyclerViewHolder(
                LayoutInflater.from(mContext).inflate(mLayoutId, parent, (false))
            )
        } else throw Exception("BaseDelegateAdapter onCreateViewHolder fail!")
    }

    override fun getItemCount(): Int =
        if (mItemCount == 0) data.size else mItemCount

    override fun getItemViewType(position: Int)
            : Int = mItemViewType

    override fun onCreateLayoutHelper()
            : LayoutHelper = mHelper

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        holder.itemView.setOnClickListener {
            if (data.size == 0) {
                return@setOnClickListener
            }
            setOnItemClick(it, position, data[position])
        }
        holder.itemView.setOnLongClickListener {
            if (data.size == 0) {
                return@setOnLongClickListener false
            }
            return@setOnLongClickListener setOnLongItemClick(it, position, data[position])
        }
        for (id: Int in getChildClickViewIds()) {
            holder.itemView.findViewById<View>(id)?.setOnClickListener {
                if (data.size == 0) {
                    return@setOnClickListener
                }
                setOnItemChildClick(it, position, data[position])
            }
            holder.itemView.findViewById<View>(id)?.setOnLongClickListener {
                if (data.size == 0) {
                    return@setOnLongClickListener false
                }
                return@setOnLongClickListener setOnLongItemChildClick(it, position, data[position])
            }
        }
        this.onBindViewHolder(holder.itemView, position)
        if (data.size != 0) {
            this.onBindViewHolder(holder.itemView, position, data[position])
        }
    }


    open fun onBindViewHolder(itemView: View, position: Int) = Unit

    open fun onBindViewHolder(itemView: View, position: Int, itemData: T) = Unit

    open fun setAddDataNew(data: MutableList<T>) {
        this.data.clear()
        this.data.addAll(data)
        this.notifyDataSetChanged()
    }

    open fun setAddDataMore(data: MutableList<T>) {
        this.data.addAll(data)
        this.notifyDataSetChanged()
    }

    open fun addItemData(data: T) {
        this.data.add(data)
        this.notifyDataSetChanged()
    }

    open fun addAllData(data: MutableList<T>) {
        this.data.addAll(data)
        this.notifyDataSetChanged()
    }

    open fun removeItemData(data: T) {
        this.data.remove(data)
        this.notifyDataSetChanged()
    }

    open fun removeItemData(position: Int) {
        this.data.removeAt(position)
        this.notifyDataSetChanged()
    }

    open fun removeAllDate() {
        this.data.clear()
        this.notifyDataSetChanged()
    }


    fun getChildClickViewIds(): LinkedHashSet<Int> {
        return childClickViewIds
    }

    /**
     * 设置需要点击事件的子view
     * @param viewIds IntArray
     */
    fun addChildClickViewIds(@IdRes vararg viewIds: Int) {
        for (viewId in viewIds) {
            childClickViewIds.add(viewId)
        }
    }

    fun setOnItemClickListener(onItemClickListener: ((View, Int, T) -> Unit)) {
        this.onItemClickListener = onItemClickListener
    }

    fun setOnLongItemClickListener(onLongItemClickListener: ((View, Int, T) -> Boolean)) {
        this.onLongItemClickListener = onLongItemClickListener
    }

    fun setOnItemChildClickListener(onItemChildClickListener: ((View, Int, T) -> Unit)) {
        this.onItemChildClickListener = onItemChildClickListener
    }

    fun setOnLongItemChildClickListener(onLongItemChildClickListener: ((View, Int, T) -> Boolean)) {
        this.onLongItemChildClickListener = onLongItemChildClickListener
    }

    fun onItemClickListener(): ((View, Int, T) -> Unit)? = onItemClickListener

    fun onLongItemClickListener(): ((View, Int, T) -> Boolean)? = onLongItemClickListener

    fun onItemChildClickListener(): ((View, Int, T) -> Unit)? = onItemChildClickListener

    fun onLongChildItemClickListener(): ((View, Int, T) -> Boolean)? = onLongItemChildClickListener

    open fun setOnItemClick(v: View, position: Int, t: T) {
        onItemClickListener?.invoke(v, position, t)
    }

    open fun setOnLongItemClick(v: View, position: Int, t: T): Boolean {
        return onLongItemClickListener?.invoke(v, position, data[position]) ?: false
    }

    open fun setOnItemChildClick(v: View, position: Int, t: T) {
        onItemChildClickListener?.invoke(v, position, t)
    }

    open fun setOnLongItemChildClick(v: View, position: Int, t: T): Boolean {
        return onLongItemChildClickListener?.invoke(v, position, data[position]) ?: false
    }

//    private var onItemTest: onItemTest<*>? = null
//
//    fun setOnItemTest(onItemTest: onItemTest<*>) {
//        this.onItemTest = onItemTest
//    }
}
//
//interface onItemTest<T> {
//    fun test(t: T)
//}